/**
 * 
 */
package com.gopawpaw.android.demo.views;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.gopawpaw.android.demo.R;

/**
 * View 转换为 Bitmap用法
 * @author Jason
 * @since: 2011-5-31
 */
public class View_Convert_Bitmap extends Activity implements OnClickListener{
	private Button buttonGen1 = null;
	private Button buttonGen2 = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.layout_view_convert_bitmap);
		
		buttonGen1 = (Button) findViewById(R.id.button_view_convert_bitmap_gen1);
		buttonGen1.setOnClickListener(this);
		
		buttonGen2 = (Button) findViewById(R.id.button_view_convert_bitmap_gen2);
		buttonGen2.setOnClickListener(this);
		
	}
	
	public void drawdraw(Canvas c){
		c.drawARGB(255, 255, 0, 0);
	}
	

	@Override
	public void onClick(View v) {

		// TODO Auto-generated method stub
		int viewID = v.getId();
		switch (viewID) {
		case R.id.button_view_convert_bitmap_gen1:
		{//View显示后 To Bitmap
			ImageButton b = new ImageButton(View_Convert_Bitmap.this){
				@Override
				public void draw(Canvas canvas) {
					// TODO Auto-generated method stub
					super.draw(canvas);
				}
				
				@Override
				protected void onDraw(Canvas canvas) {
					// TODO Auto-generated method stub
					super.onDraw(canvas);
				}
				
			};
			
			//关键代码========================================================<
			/**一般搭配使用
			 * 1:
			 *		v1.setDrawingCacheEnabled(true);
			 *	和	v1.getDrawingCache();
			 * 2:
			 * 		v1.buildDrawingCache(); 
			 * 和	v1.getDrawingCache();
			 * 
			 * 单独使用 v1.getDrawingCache();一般bm都会为null
			 * 不要使用 v1.getDrawingCache(true/false);
			**/
			
			View v1 = v.getRootView();
			
			//设置允许绘画缓存
			v1.setDrawingCacheEnabled(true);
			
//			v1.buildDrawingCache();
			
			//获取绘画缓存Bitmap
			Bitmap bm = v1.getDrawingCache();
			
			//关键代码========================================================>
			
			if(bm == null){
				Log.d("jason", "bm == null");
			}
			
			b.setImageBitmap(bm);
			
			ScrollView sv = new ScrollView(getApplicationContext());
			
			sv.addView(b);
			
			Dialog dialog = new Dialog(View_Convert_Bitmap.this);
			dialog.setTitle("View 已经显示 转换为 Bitmap 所显示结果");
			dialog.setContentView(sv);
			dialog.show();
		}
			break;
		case R.id.button_view_convert_bitmap_gen2:
		{//View显示前 To Bitmap
			View view = new View(this){
	        	protected void onDraw(android.graphics.Canvas canvas) {
	        		Log.d("jason", "onDraw");
	        		
	        		LinearLayout ll = new LinearLayout(View_Convert_Bitmap.this);
	        		ll.setOrientation(LinearLayout.HORIZONTAL);

	        		ImageView iv = new ImageView(View_Convert_Bitmap.this);
	                iv.setBackgroundResource(R.drawable.icon);

	                TextView tv = new TextView(View_Convert_Bitmap.this);
	                tv.setText("this is TextView");
	                
	                Button bv = new Button(View_Convert_Bitmap.this);
	                bv.setText("this is Button");

	                ll.addView(iv);
	                ll.addView(tv);
	                ll.addView(bv);
	                
	                //测量其子View的大小位置
	                ll.measure(400, 400);
	                
	                ll.layout(0, 0, 400, 400);

	              ll.setDrawingCacheEnabled(true);
	        		
	              Bitmap bm = ll.getDrawingCache();
	        		
	              canvas.drawBitmap(bm, 0, 0, new Paint());
	        	};
	        	
	        };
			
			Dialog dialog = new Dialog(View_Convert_Bitmap.this);
			dialog.setTitle("View 未显示 转换为 Bitmap 所显示结果");
			dialog.setContentView(view);
			dialog.show();
		}
			break;
			
		default:
			break;
		}
		
		
	
	}
}
